//
//  YGMapView.m
//  map
//
//  Created by 黄德玉 on 2017/2/15.
//  Copyright © 2017年 黄德玉. All rights reserved.
//

#import "YGMapView.h"

@interface YGMapView ()<MAMapViewDelegate>
@property(nonatomic,strong) MAMapView * mapView;            //地图
@property(nonatomic,strong) UIView * zoomPannelView;        // + -
@property(nonatomic,strong) UIButton * gpsBtn;              //定位到当前位置
@property(nonatomic,strong) MAPointAnnotation * fixCenterPin; //固定在中心的按钮
@p'r
@end

@implementation YGMapView


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.mapView];
        [self addSubview:self.zoomPannelView];
        [self addSubview:self.gpsBtn];
    }
    return self;
}

#pragma mark - action
-(void)zoomPlusAction{
    CGFloat oldZoom = self.mapView.zoomLevel;
    [self.mapView setZoomLevel:(oldZoom + 1) animated:YES];
}
-(void)zoomMinusAction{
    CGFloat oldZoom = self.mapView.zoomLevel;
    [self.mapView setZoomLevel:(oldZoom - 1) animated:YES];
}
-(void)gpsBtnClicked{
    if(self.mapView.userLocation.updating && self.mapView.userLocation.location) {
        [self.mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate animated:YES];
        [self.gpsBtn setSelected:YES];
    }
}
#pragma mark - delegate
/**
 * @brief 地图区域即将改变时会调用此接口
 * @param mapView 地图View
 * @param animated 是否动画
 */
- (void)mapView:(MAMapView *)mapView regionWillChangeAnimated:(BOOL)animated{
    NSLog(@"地图区域即将改变");
}

/**
 * @brief 地图区域改变完成后会调用此接口
 * @param mapView 地图View
 * @param animated 是否动画
 */
- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated{  //地图操作，最后一个调用函数
    NSLog(@"地图区域改变完成");
   // self.fixCenterPin.coordinate = mapView.centerCoordinate;
    [self.delegate centerChangedWithLocation:self.fixCenterPin.coordinate];
}
//
///**
// * @brief 地图将要发生移动时调用此接口
// * @param mapView 地图view
// * @param wasUserAction 标识是否是用户动作
// */
- (void)mapView:(MAMapView *)mapView mapWillMoveByUser:(BOOL)wasUserAction{
    NSLog(@"地图区域将要发生移动");
}
//
///**
// * @brief 地图移动结束后调用此接口
// * @param mapView 地图view
// * @param wasUserAction 标识是否是用户动作
// */
- (void)mapView:(MAMapView *)mapView mapDidMoveByUser:(BOOL)wasUserAction{
    NSLog(@"地图区域移动结束");
}
//
///**
// * @brief 地图将要发生缩放时调用此接口
// * @param mapView       地图view
// * @param wasUserAction 标识是否是用户动作
// */
- (void)mapView:(MAMapView *)mapView mapWillZoomByUser:(BOOL)wasUserAction{
    NSLog(@"地图将要缩放");
}
//
///**
// * @brief 地图缩放结束后调用此接口
// * @param mapView       地图view
// * @param wasUserAction 标识是否是用户动作
// */
- (void)mapView:(MAMapView *)mapView mapDidZoomByUser:(BOOL)wasUserAction{
    NSLog(@"地图缩放完成");
}
//
///**
// * @brief 单击地图底图调用此接口
// * @param mapView    地图View
// * @param coordinate 点击位置经纬度
// */
- (void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate{
   // [self.mapView setCenterCoordinate:coordinate animated:YES];
    NSLog(@"单击地图");
}
//
///**
// * @brief 长按地图底图调用此接口
// * @param mapView    地图View
// * @param coordinate 长按位置经纬度
// */
- (void)mapView:(MAMapView *)mapView didLongPressedAtCoordinate:(CLLocationCoordinate2D)coordinate{
    
    NSLog(@"长按地图");
}
//
///**
// * @brief 根据anntation生成对应的View
// * @param mapView 地图View
// * @param annotation 指定的标注
// * @return 生成的标注View
// */
- (MAAnnotationView*)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIdentifier = @"pointReuseIdentifier";
        MAPinAnnotationView *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIdentifier];
        if (annotationView == nil){
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIdentifier];
            annotationView.canShowCallout = YES;
            annotationView.animatesDrop = YES;
            annotationView.draggable = YES;
            //    annotationView.image = [UIImage imageNamed:@"gpsStat1"];
        }
        
        //annotationView.pinColor = (annotation == self.annotations.firstObject ? MAPinAnnotationColorRed : MAPinAnnotationColorGreen);
        
        return annotationView;
    }
    return nil;
}
//
///**
// * @brief 当mapView新添加annotation views时调用此接口
// * @param mapView 地图View
// * @param views 新添加的annotation views
// */
//- (void)mapView:(MAMapView *)mapView didAddAnnotationViews:(NSArray *)views{
//
//}
//
///**
// * @brief 当选中一个annotation views时调用此接口
// * @param mapView 地图View
// * @param view 选中的annotationView
// */
//- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view{
//
//}
//
///**
// * @brief 当取消选中一个annotation views时调用此接口
// * @param mapView 地图View
// * @param view 取消选中的annotationView
// */
//- (void)mapView:(MAMapView *)mapView didDeselectAnnotationView:(MAAnnotationView *)view{
//
//}
//
///**
// * @brief 标注view的accessory view(必须继承自UIControl)被点击时调用此接口
// * @param mapView 地图View
// * @param view callout所属的标注view
// * @param control 对应的control
// */
//- (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
//
//}
//
///**
// * @brief 标注view的calloutview整体点击时调用此接口
// * @param mapView 地图的view
// * @param view calloutView所属的annotationView
// */
//- (void)mapView:(MAMapView *)mapView didAnnotationViewCalloutTapped:(MAAnnotationView *)view{
//
//}
//
///**
// * @brief 在地图View将要启动定位时调用此接口
// * @param mapView 地图View
// */
- (void)mapViewWillStartLocatingUser:(MAMapView *)mapView{
    NSLog(@"将要启动过定位");
}
//
///**
// * @brief 在地图View停止定位后调用此接口
// * @param mapView 地图View
// */
- (void)mapViewDidStopLocatingUser:(MAMapView *)mapView{
    NSLog(@"停止定位");
}
//
///**
// * @brief 位置或者设备方向更新后调用此接口
// * @param mapView 地图View
// * @param userLocation 用户定位信息(包括位置与设备方向等数据)
// * @param updatingLocation 标示是否是location数据更新, YES:location数据更新 NO:heading数据更新
// */
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
  //  NSLog(@"位置发生改变");
}
//
///**
// * @brief 定位失败后调用此接口
// * @param mapView 地图View
// * @param error 错误号，参考CLError.h中定义的错误号
// */
- (void)mapView:(MAMapView *)mapView didFailToLocateUserWithError:(NSError *)error{
    NSLog(@"定位失败");
}
//
///**
// * @brief 当userTrackingMode改变时调用此接口
// * @param mapView 地图View
// * @param mode 改变后的mode
// * @param animated 动画
// */
- (void)mapView:(MAMapView *)mapView didChangeUserTrackingMode:(MAUserTrackingMode)mode animated:(BOOL)animated{
    NSLog(@"改变用户跟踪状态");
}
//
///**
// * @brief 拖动annotation view时view的状态变化，ios3.2以后支持
// * @param mapView 地图View
// * @param view annotation view
// * @param newState 新状态
// * @param oldState 旧状态
// */
//- (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view didChangeDragState:(MAAnnotationViewDragState)newState fromOldState:(MAAnnotationViewDragState)oldState{
//
//}
//
///**
// * @brief 根据overlay生成对应的Renderer
// * @param mapView 地图View
// * @param overlay 指定的overlay
// * @return 生成的覆盖物Renderer
// */
//- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay{
//    return nil;
//}
//
///**
// * @brief 当mapView新添加overlay renderer时调用此接口
// * @param mapView 地图View
// * @param renderers 新添加的overlay renderers
// */
//- (void)mapView:(MAMapView *)mapView didAddOverlayRenderers:(NSArray *)renderers{
//
//}


#pragma mark - 初始化
-(MAMapView *)mapView{
    if (!_mapView) {
        _mapView = [[MAMapView alloc] initWithFrame:self.bounds];
        _mapView.delegate = self;
       // _mapView.mapType = MAMapTypeSatellite;
       // _mapView.language = MAMapLanguageZhCN;
        _mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
       // _mapView.showsCompass= YES; // 设置成NO表示关闭指南针；YES表示显示指南针
       // _mapView.compassOrigin= CGPointMake(_mapView.compassOrigin.x, 22); //设置指南针位置
        //_mapView.showsScale =  YES; //是否显示比例尺
       // _mapView.scaleOrigin= CGPointMake(_mapView.scaleOrigin.x, 22);  //设置比例尺位置
      //  _mapView.zoomLevel = 12;
        _mapView.showsUserLocation = YES;
        _mapView.userTrackingMode = MAUserTrackingModeFollow;
       // _mapView.mapType = MAMapTypeNavi;
        [_mapView addAnnotation:self.fixCenterPin];       //添加一个Pin
    }
    return _mapView;
}

-(UIView *)zoomPannelView{
    if (!_zoomPannelView) {
        _zoomPannelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 53, 98)];
        _zoomPannelView.center = CGPointMake(self.frame.size.width - 30, self.frame.size.height - 50);
        UIButton *incBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 53, 49)];
        [incBtn setImage:[UIImage imageNamed:@"increase"] forState:UIControlStateNormal];
        [incBtn sizeToFit];
        [incBtn addTarget:self action:@selector(zoomPlusAction) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *decBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 49, 53, 49)];
        [decBtn setImage:[UIImage imageNamed:@"decrease"] forState:UIControlStateNormal];
        [decBtn sizeToFit];
        [decBtn addTarget:self action:@selector(zoomMinusAction) forControlEvents:UIControlEventTouchUpInside];
        
        [_zoomPannelView addSubview:incBtn];
        [_zoomPannelView addSubview:decBtn];
    }
    return _zoomPannelView;
}
-(UIButton *)gpsBtn{
    if (!_gpsBtn) {
        _gpsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _gpsBtn.frame = CGRectMake(0, 0, 40, 40);
        _gpsBtn.center = CGPointMake(30, self.frame.size.height - 40);
        _gpsBtn.backgroundColor = [UIColor whiteColor];
        _gpsBtn.layer.cornerRadius = 4.0;
        [_gpsBtn setImage:[UIImage imageNamed:@"gpsStat1"] forState:UIControlStateNormal];
        [_gpsBtn addTarget:self action:@selector(gpsBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _gpsBtn;
}
-(MAPointAnnotation *)fixCenterPin{
    if (!_fixCenterPin) {
        _fixCenterPin = [[MAPointAnnotation alloc] init];
        _fixCenterPin.lockedToScreen = YES;
        _fixCenterPin.lockedScreenPoint = CGPointMake(self.frame.size.width * .5, self.frame.size.height * .5);
        _fixCenterPin.title = @"这是位置";
        _fixCenterPin.subtitle = @"这是子位置";
        //        CLLocationCoordinate2D coordinates = {31.216912,121.349314};
        //        _centerPosition.coordinate  = coordinates;
    }
    return _fixCenterPin;
}
@end
